package com.kfm.builder;

import com.kfm.model.Account;
import org.apache.ibatis.jdbc.SQL;

public class AccountSQLBuilder {

    public static String selectAll(final Account account){
        return new SQL(){
            {
                SELECT("id", "user_id"); // 查询列
                SELECT("money");
                FROM("account");
                if (account != null){
                    if (account.getId() != null){
                        WHERE("id = #{id}");
                    }
                    if (account.getUserId() != null){
                        WHERE("user_id = #{userId}");
                    }
                    if (account.getMoney() != null){
                        OR().WHERE("money = #{money}");
//                        WHERE("money = #{money}");
                    }
                }
            }
        }.toString();
    }

    public static String update(final Account account){
        SQL sql = new SQL();
        sql.UPDATE("account");

        if (account != null){
            if (account.getUserId() != null){
                sql.SET("user_id = #{userId}");
            }
            if (account.getMoney() != null){
                sql.SET("money = #{money}");
            }
        }

        sql.WHERE("id = #{id}");
        return sql.toString();
    }

    public static String insert(final Account account){
        SQL sql = new SQL();

        // insert into account(id) values(#{id})
        sql.INSERT_INTO("account");

        if (account != null){
            if (account.getId() != null){
//                sql.INTO_COLUMNS("id");
//                sql.INTO_VALUES("#{id}");

                sql.VALUES("id", "#{id}");
            }
            if (account.getMoney() != null){
                sql.VALUES("money", "#{money}");
            }
            if (account.getUserId() != null){
                sql.VALUES("user_id", "#{userId}");
            }
        }

        return sql.toString();
    }
}
